package cn.yza.设计模式.单例模式;

import java.util.concurrent.atomic.AtomicLong;

/** 双重检测
 * @author Honglixi
 * @create 2021-10-25 14:03
 */
public class SingletonTest03 {
    private AtomicLong id = new AtomicLong(0);

    private static volatile SingletonTest03 instance;

    private SingletonTest03() {}

    public static SingletonTest03 getInstance() {
        if (instance == null) {
            synchronized(SingletonTest03.class) { // 此处为类级别的锁
                if (instance == null) {
                    instance = new SingletonTest03();
                }
            }
        }
        return instance;
    }
    public long getId() {
        return id.incrementAndGet();
    }
}
